#!/bin/ksh

function errexit
{
    echo "error: $@" >&2
    exit 1
}

cwd=$(pwd)
src_dir=$(dirname $(readlink -f $0))
top_dir=$(readlink -f $(dirname ${src_dir}))
bin_dir=${top_dir}/bin
build_dir=${top_dir}/build
test_dir=${top_dir}/test

CC=gcc
CFLAGS="-Wall -mtune=i686 -O3 -I."

[ -d ${bin_dir} ] || mkdir -p ${bin_dir}
[ -d ${build_dir} ] || mkdir -p ${build_dir}

cd ${src_dir}
sources="jobs.h utils.h utils.c scan_job.h scan_job.l parse_job.y"
for src_file in ${sources}; do
  [ -f ${src_file} ] || errexit "${src_file}: source file missing"
  cp -a ${src_file} ${build_dir}
done

cd ${build_dir}

bison -d -o parse_job.h parse_job.y
[ $? -ne 0 ] && errexit "parse_job.h: header generation failed"

bison -o parse_job.c parse_job.y
[ $? -ne 0 ] && errexit "parse_job.c: bison parsing failed"

flex -8 -i -oscan_job.c scan_job.l
[ $? -ne 0 ] && errexit "scan_job.c: flex lexical analysis failed"

${CC} -g -c ${CFLAGS} -DDEBUG -DTESTSCAN -o scan_job.o scan_job.c
[ $? -ne 0 ] && errexit "scan_job.o: first debug compilation failed"

${CC} -g ${CFLAGS} -o dbg.scan_job scan_job.o -lfl
[ $? -ne 0 ] && errexit "scan_job: debug compilation failed"

${CC} -g -c ${CFLAGS} -o scan_job.o scan_job.c
[ $? -ne 0 ] && errexit "scan_job.o: second debug compilation failed"

${CC} -g -c ${CFLAGS} -o utils.o utils.c
[ $? -ne 0 ] && errexit "utils.o: debug compilation failed"

${CC} -g -c ${CFLAGS} -DDEBUG -DTESTPARSE -o parse_job.o parse_job.c
[ $? -ne 0 ] && errexit "parse_job.o: first debug compilation failed"

${CC} -g ${CFLAGS} -o dbg.parse_job parse_job.o scan_job.o utils.o -lfl
[ $? -ne 0 ] && errexit "parse_job: debug compilation failed"

${CC} -c ${CFLAGS} -DTESTSCAN -o scan_job.o scan_job.c
[ $? -ne 0 ] && errexit "scan_job.o: second compilation failed"

${CC} ${CFLAGS} -o scan_job scan_job.o -lfl
[ $? -ne 0 ] && errexit "scan_job: second compilation failed"

${CC} -c ${CFLAGS} -o scan_job.o scan_job.c
[ $? -ne 0 ] && errexit "scan_job.o: final compilation failed"

${CC} -c ${CFLAGS} -o utils.o utils.c
[ $? -ne 0 ] && errexit "utils.o: final compilation failed"

${CC} -c ${CFLAGS} -DTESTPARSE -o parse_job.o parse_job.c
[ $? -ne 0 ] && errexit "parse_job.o: final compilation failed"

${CC} ${CFLAGS} -o parse_job parse_job.o scan_job.o utils.o -lfl
[ $? -ne 0 ] && errexit "parse_job: final compilation failed"

cp -a dbg.scan_job ${bin_dir}/debug/scan_job
cp -a dbg.parse_job ${bin_dir}/debug/parse_job
cp -a scan_job ${bin_dir}
cp -a parse_job ${bin_dir}

read prompt?"Performing basic test... Press <RETURN> to continue... "
test_job=${test_dir}/all-jobs/job-0001.txt
if [ -f ${test_job} ]; then
    echo "------------------------------------"
    cat ${test_job}
    echo "------------------------------------"
    ${bin_dir}/scan_job ${test_job} 2>/dev/null
    echo "------------------------------------"
    ${bin_dir}/parse_job ${test_job} 2>/dev/null
    echo "------------------------------------"
else
    errexit "${test_job}: no test job found"
fi

read prompt?"Performing atomic tests... Press <RETURN> to continue... "
for test_job in ${test_dir}/all-jobs/job-*.txt; do
    echo -n "${test_job}"
    ${bin_dir}/parse_job ${test_job} >/dev/null 2>&1
    if [ $? -eq 0 ]; then
        echo -e "\033[60G\033[32m[OK]\033[0m"
    else
        echo -e "\033[60G\033[31m[FAILED]\033[0m"
    fi
done

read prompt?"Performing combined test... Press <RETURN> to continue... "
${bin_dir}/parse_job ${test_dir}/job.txt 2>/dev/null | less

cd ${cwd}
exit 0

